<form class="layui-form" action="{:url('',['stall_feed_id'=>$stall_feed_id])}" method="post" id="editForm" lay-filter="edit-form">
    
    {:FormExt::input('费用单类型', 'stall_cost_type', '', '请输入费用单类型', 'required', '')}
    {:FormExt::input('费用单金额', 'stall_cost_money', '', '请输入费用单金额', 'required', 'lay-verify="number"')}
    <div class="layui-form-item layui-form-text">
	    <label class="layui-form-label">上传凭证</label>
	    <div class="layui-inline upload" style="width:280px;">
            <button type="button" name="upload" class="layui-btn layui-btn-primary layui-upload" lay-type="image" lay-data="{exts:'jpg|png|gif|bmp|jpeg', accept:'images'}">+ 上传图片</button>
            <input type="hidden" class="upload-input" name="pic" value="">
            <span class="lay-pic-container" src="">
                <img src="" class="lay-pic" style="display:none;border-radius:5px;border:1px solid #ccc" width="36" height="36">
            </span>
            
<!--            <img src="" >-->
        </div>
	</div>
    
    {:FormExt::textarea('备注', 'remark', '', '请输入备注', '', 'lay-verify="required"')}
    <div class="pop-bottom-bar">
<!--    	<input type="hidden" class="field-id" name="id">-->
        <button type="submit" class="layui-btn layui-btn-normal" lay-submit="" lay-filter="formSubmit" hisi-data="{pop: true, refresh: false, callback: callbackUpdateStock}">提交保存</button>
        <a href="javascript:parent.layui.layer.closeAll();" class="layui-btn layui-btn-primary ml10">取消</a>
    </div>
</form>

{include file="system@block/layui" /}
    
<script>
layui.use(['tool', 'form', 'layer', 'upload'], function() {
	var $ = layui.jquery;
	var form = layui.form;
	var layer = layui.layer;
	var upload = layui.upload;
	$('input[name=stock]').focus();
	upload.render({
        elem: '.layui-upload',
        url: '{:url("system/annex/upload?thumb=no&water=no&full_path=true")}'
        ,method: 'post'
        ,before: function(input) {
            layer.msg('文件上传中...', {time:3000000});
        },done: function(res, index, upload) {
            var obj = this.item;
            if (res.code == 0) {
                layer.msg(res.msg);
                return false;
            }
            layer.closeAll();
            var input = $(obj).parents('.upload').find('.upload-input');
            if ($(obj).attr('lay-type') == 'image') {
                //$('.lay-pic-container').attr('src', res.data.file);
                input.siblings('.lay-pic-container').attr('src', res.data.file).show();
                $('.lay-pic').attr('src', res.data.file).show();
            }
            input.val(res.data.file);
        }
    });
    var reg = /((^[1-9]\d*)|^0)(\.\d{0,2}){0,1}$/;
    form.verify({
        mimonumber: function(value, item){
            if (value=='') return '';
            if(! reg.test(value)){
                return '只能填写数字';
            }
        }
    });
	window.calAmount = function(){
		var d = form.val("edit-form");
		var stock = 0;
		var unitPrice = 0;
		var reg = /((^[1-9]\d*)|^0)(\.\d{0,2}){0,1}$/;
		
		if (d.stock != '') {
			if (!reg.test(d.stock)) {
				layer.msg('数量格式错误', {icon: 2,anim: 6});
				return false;
			}
			stock = layui.tool.parseFloat(d.stock, 2);
		}
		if (d.unit_price != '') {
			if (!reg.test(d.unit_price)) {
				layer.msg('单价格式错误', {icon: 2,anim: 6});
				return false;
			}
			unitPrice = layui.tool.parseFloat(d.unit_price, 2);
		}
		
		$('input[name=amount]').val(layui.tool.bcmul(stock, unitPrice));
	};
	
	$(document).on('blur','input[name=stock],input[name=unit_price]',function(){
		calAmount();
	});
	
	// 表单提交回调
	window.callbackUpdateStock = function(obj, res) {
		if (res.code == 1) layer.msg(res.msg, {icon: 1,end:function(){parent.layui.layer.closeAll();}});
		var stock = 0;
		var mid = 0;
		if (mid > 0){
			// 更新采购进度库存数
			if (typeof(eval(parent.window.updateStock))==='function'){
				parent.window.updateStock(mid, res.data.stock);
			}
		}
		
		// 更新补料任务状态
		if (typeof(eval(parent.window.updateDistributingFeedStatus))==='function'){
			parent.window.updateDistributingFeedStatus(res.data.feed_id);
		}
		
		// 更新退料进度状态
		if (typeof(eval(parent.window.updateMaterialReturnStatus))==='function'){
			parent.window.updateMaterialReturnStatus(res.data.material_return_id);
		}
	}
});
</script>